#include "window_win32.h"

//-- Process messages sent to the window
LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
{
	switch(umessage)
	{
		//-- Check if the window is being destroyed.
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}

		//-- Check if the window is being closed.
	case WM_CLOSE:
		{
			PostQuitMessage(0);		
			return 0;
		}

		//-- All other messages pass to the message handler in the system class.
	default:
		{
			return DefWindowProc(hwnd, umessage, wparam, lparam);
		}
	}
}

namespace fort
{

WindowWin32::WindowWin32(HINSTANCE hinstance, int pScreen_width, int pScreen_height, LPCSTR pAppliction_name, bool pFullscreen, bool pShow_cursor) :
hwnd(NULL)
{
	DEVMODE screen_settings;
	int wnd_pos_x, wnd_pos_y;

	bits_per_pixel = 32;

	//-- Get the instance of this application.
	hinstance = hinstance;

	//-- Give the application a name.
	application_name = pAppliction_name;

	//-- Setup the windows class with default settings.
	WNDCLASSEX wc;
	memset(&wc, 0, sizeof(wc));
	wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hinstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_WINLOGO);
	wc.hIconSm       = wc.hIcon;
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = application_name;
	wc.cbSize        = sizeof(WNDCLASSEX);

	//-- Register the window class.
	RegisterClassEx(&wc);

	fullscreen = pFullscreen;
	//-- Setup the screen settings depending on whether it is running in full screen or in windowed mode.
	if(fullscreen)
	{
		//-- Determine the resolution of the source desktop screen.
		pScreen_width  = GetSystemMetrics(SM_CXSCREEN);
		pScreen_height = GetSystemMetrics(SM_CYSCREEN);

		width = pScreen_width;
		height = pScreen_height;

		//-- If full screen set the screen to maximum size of the users desktop and 32bit.
		memset(&screen_settings, 0, sizeof(screen_settings));
		screen_settings.dmSize       = sizeof(screen_settings);
		screen_settings.dmPelsWidth  = (unsigned long)pScreen_width;
		screen_settings.dmPelsHeight = (unsigned long)pScreen_height;
		screen_settings.dmBitsPerPel = bits_per_pixel;			
		screen_settings.dmFields     = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

		//-- Change the display settings to full screen.
		ChangeDisplaySettings(&screen_settings, CDS_FULLSCREEN);

		//-- Set the position of the window to the top left corner.
		wnd_pos_x = wnd_pos_y = 0;
	}
	else
	{
		width = pScreen_width;
		height = pScreen_height;

		pScreen_width  += (int)GetSystemMetrics(SM_CXSIZEFRAME)*2;
		pScreen_height += (int)GetSystemMetrics(SM_CXSIZEFRAME)*2+(int)GetSystemMetrics(SM_CYCAPTION);

		//-- Place the window in the middle of the screen.
		if((int)GetSystemMetrics(SM_CXSCREEN) < pScreen_width)
			wnd_pos_x = 0;
		else
			wnd_pos_x = (int)(GetSystemMetrics(SM_CXSCREEN) - pScreen_width) >> 1;

		if((int)GetSystemMetrics(SM_CYSCREEN) < pScreen_height)
			wnd_pos_y = 0;
		else
			wnd_pos_y = (int)(GetSystemMetrics(SM_CYSCREEN) - pScreen_height) >> 1;
	}

	//-- Create the window with the screen settings and get the handle to it.
	if(fullscreen)
	{
		hwnd = CreateWindowEx(WS_EX_TOPMOST, application_name, application_name, 
			WS_POPUP,
			wnd_pos_x, wnd_pos_y, pScreen_width, pScreen_height, NULL, NULL, hinstance, NULL);
	}
	else
	{
		hwnd = CreateWindowEx(WS_EX_APPWINDOW, application_name, application_name, 
			WS_OVERLAPPEDWINDOW,
			wnd_pos_x, wnd_pos_y, pScreen_width, pScreen_height, NULL, NULL, hinstance, NULL);
	}

	//-- Bring the window up on the screen and set it as main focus.
	ShowWindow(hwnd, SW_SHOW);
	SetForegroundWindow(hwnd);
	SetFocus(hwnd);

	//-- Show/hide the mouse cursor.
	show_cursor = pShow_cursor;
	ShowCursor(show_cursor);
}

WindowWin32::~WindowWin32()
{

	//-- Fix the display settings if leaving full screen mode.
	if(fullscreen)
		ChangeDisplaySettings(NULL, 0);

	//-- Remove the window.
	DestroyWindow(hwnd);
	hwnd = NULL;

	//-- Remove the application instance.
	UnregisterClass(application_name, hinstance);
	hinstance = NULL;
}

}